package com.example.demo.service;/**
 * ClassName: SubscribeService
 * Package: com.example.demo.services
 * Description:
 *
 * @Author 理理
 * @Create 2024/11/16 19:37
 * @Version 1.0
 */

import com.example.demo.entity.Subscribe;
import com.example.demo.entity.TimeSlotAvailability;
import com.example.demo.mapper.ClassesMapper;
import com.example.demo.mapper.SubscribeMapper;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 *
 *@title: SubscribeService
 *@Author: Tangerine
 *@Date: 2024/11/16 19:37
 *@version 1.0
 */
@Service
public class SubscribeService {
    @Autowired
    private SubscribeMapper subscribeMapper;

    @Autowired
    private TimeQuantumService timeQuantumService;

    @Autowired
    private TimeSlotAvailabilityService timeSlotAvailabilityService;


    public SubscribeService(TimeSlotAvailabilityService timeSlotAvailabilityService){
        this.timeSlotAvailabilityService = timeSlotAvailabilityService;
    }

    //分页查询数据
    public List<Subscribe> selectPage(Integer pageNum,Integer pageSize){
        return subscribeMapper.selectPage(pageNum,pageSize);
    }

    //更新或插入一条预约
    public int save(Subscribe subscribe){
        //如果没有id则表明是新增数据
        if(subscribe.getId()==null){
            TimeSlotAvailability temp = timeSlotAvailabilityService.con(subscribe.getClasses_name(),subscribe.getDate(),subscribe.getTime());
            if(subscribe.getType().equals("个人预约")){     //审核个人预约
                if(temp.getAvailable()){      //成功
                    return subscribeMapper.insert(subscribe);
                }else {     //失败
                    return 0;
                }
            }else{      //审核班级预约
                if(temp.getSurplus() < temp.getCount()){      //失败
                    return 0;
                }else {      //成功
                    return subscribeMapper.insert(subscribe);


                }
            }
        } else {//否则就是更新数据
            return subscribeMapper.update(subscribe);
        }
    }

    //删除一条预约
    public Integer deleteById(Integer id) {
        return subscribeMapper.deleteById(id);
    }

    //处理一条预约信息
    public int checkS(Subscribe subscribe){

        TimeSlotAvailability temp = timeSlotAvailabilityService.con(subscribe.getClasses_name(),subscribe.getDate(),subscribe.getTime());
        if(subscribe.getState().equals("预约成功")){  //预约成功分情况审核
            if(subscribe.getType().equals("个人预约")){     //审核个人预约
                if(temp.getAvailable()){      //成功
                    subscribe.setResult("恭喜您，预约成功！");
                    return subscribeMapper.update(subscribe);
                }else {     //失败
                    subscribe.setState("预约失败");
                    subscribe.setResult("实验室已满员，请重新预约");
                    return subscribeMapper.update(subscribe);
                }
            }else{      //审核班级预约
                if(temp.getSurplus() < temp.getCount()){      //失败
                    subscribe.setState("预约失败");
                    subscribe.setResult("实验室已满员，请重新预约");
                    return subscribeMapper.update(subscribe);
                }else {      //成功
                    subscribe.setResult("预约成功，请按时使用");
                    return subscribeMapper.update(subscribe);

                }
            }
        }else{ //预约失败只返回提示信息
            if(subscribe.getType().equals("个人预约")){
                //改变预约状态  修改预约返回信息
                subscribe.setResult("实验室已满员，请重新预约");
                subscribeMapper.update(subscribe);
                return 0;
            }else{
                subscribe.setType("预约失败");
                subscribe.setResult("实验室已被使用，请重新预约");
                subscribeMapper.update(subscribe);
                return 0;
            }
        }
    }

    //查询不同类型的所有数据
    public List<Subscribe> labTypeSelect(Integer labType){
        return subscribeMapper.labTypeSelect(labType);
    }
    //查询不同类型的所有数据,分页显示
    public List<Subscribe> labTypeSelectPage(Integer labType,Integer pageNum,Integer pageSize){
        return subscribeMapper.labTypeSelectPage(labType,pageNum,pageSize);
    }
    //分类查询（预约类型、预约状态）
    public List<Subscribe> typeSelect(String temp){
        return subscribeMapper.typeSelect(temp);
    }
    public List<Subscribe> stateSelect(String temp){
        return subscribeMapper.stateSelect(temp);
    }

    //历史预约全部
    public List<Subscribe> findHistory(String studentNumber){
        List<Subscribe> subList = subscribeMapper.findHistory(studentNumber);
        for(Subscribe temp : subList){
            check(temp);
        }
        return subList;
    }
    //历史预约，分页显示
    public List<Subscribe> findHistoryPage(String studentNumber,Integer pageNum,Integer pageSize){
        List<Subscribe> subList = subscribeMapper.findHistoryPage(studentNumber,pageNum,pageSize);
        for(Subscribe temp : subList){
            check(temp);
        }
        return subList;
    }

    //检查预约成功的预约是否已完成
    public Subscribe check(Subscribe temp){
        LocalDateTime systemTime = LocalDateTime.now();//系统时间
        Time time1 = timeQuantumService.getEnd(temp.getTime()); //当前记录的时间段的结束时间
        LocalDate dataPart = temp.getDate().toLocalDate();
        LocalTime timePart = time1.toLocalTime();
        LocalDateTime dbDataTime = LocalDateTime.of(dataPart,timePart); //合并后的数据库时间
        if(systemTime.isAfter(dbDataTime)) {
            subscribeMapper.alterState(temp.getId());
        }
        return temp;
    }

    //根据管理员姓名找所有预约记录里他所管理的实验室
    public List<Subscribe> getAdminClasses(String manager){
        List<Subscribe> subList = subscribeMapper.getAdminClasses(manager);
        for(Subscribe temp : subList){
            check(temp);
        }
        return subList;
    }
}
